package com.xintoucloud.pbs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.pbs.entity.Organization;
import com.xintoucloud.pbs.entity.PartyMember;
import com.xintoucloud.pbs.model.OrganizationInformationDto;
import com.xintoucloud.pbs.model.OrganizationNode;
import com.xintoucloud.pbs.model.TreeData;
import com.xintoucloud.pbs.model.TreeNode;
import com.xintoucloud.pbs.service.OrganizationService;

@Controller@RequestMapping("/organization")
public class OrganizationController {
	@Autowired 
	private OrganizationService organizationService;
	@GetMapping("/showOrganizationTree")
	public String showOrganizationTreeView() {
		 return "organizationTree";
	}
	@GetMapping("/showOrganizationTree_2")
	public String showOrganizationTreeView_2() {
		return "organizationTree2";
	}
	/*@RequestMapping("/showOrganizationTree2")
	@ResponseBody
	public TreeData showOrganizationTreeView2(HttpSession session) {
		PartyMember partyMember=(PartyMember) session.getAttribute("loginPartyMember");
		int ppid=partyMember.getPmp().getPpid();
		int oid=partyMember.getOrg().getOid();
		TreeData treeData=null;
		if(ppid==1||ppid==2) {
		treeData= organizationService.getTreeData(0);
		}else if(ppid==3) {
		treeData= organizationService.getTreeData(oid);
		}else if(ppid==4) {
			treeData= organizationService.getTreeData(oid);
		}
		return treeData;
	}*/
	@RequestMapping("/showOrganizationTree2")
	@ResponseBody
	public TreeData showOrganizationTreeView2(HttpSession session) {
		PartyMember partyMember=(PartyMember) session.getAttribute("loginPartyMember");
		int ppid=partyMember.getPmp().getPpid();
		int oid=partyMember.getOrg().getOid();
		TreeData treeData=null;
		if(ppid==1||ppid==2) {
			treeData= organizationService.getTreeData(0);
		}else if(ppid==3) {
			treeData= organizationService.getTreeData(oid);
		}else if(ppid==4) {
			treeData= organizationService.getTreeData(oid);
		}
		return treeData;
	}
	
	@PostMapping("/showOrganizationTree")
	@ResponseBody
	public List<OrganizationNode> showOrganizationTree() {
		 List<OrganizationNode> organizationNodes= organizationService.listOrganizationByParentId(0);
		 return organizationNodes;
	}
	@ResponseBody
	@PostMapping("/deleteOrganization")
	public boolean deleteOrganization(int oid) {
		return organizationService.deleteById(oid);
	}
	@ResponseBody
	@RequestMapping("/updateOrganization")
	public TreeNode updateOrganization(int oid,String oName) {
		TreeNode treeNode= organizationService.updateOrganization(oid,oName);		
		return treeNode;
	}
	@ResponseBody
	@PostMapping("/addOrganization")
	public int addOrganization(int parentId,String oName) {
		Organization org=organizationService.saveOrganization(parentId,oName);
		return org.getOid();
	}
	@RequestMapping("/showOrganizationInformation")
	public String showOrganizationInformation(HttpSession session,Model m) {
		PartyMember partyMember=(PartyMember) session.getAttribute("loginPartyMember");
		int oid=partyMember.getOrg().getOid();
		OrganizationInformationDto orginfd=organizationService.showOrganizationInformation(oid);
		m.addAttribute("orginfd", orginfd);
		return "organizationInformation";
	}
	@RequestMapping("/editOrganizationInformation")
	public String editOrganizationInformation(HttpSession session,Model m) {
		PartyMember partyMember=(PartyMember) session.getAttribute("loginPartyMember");
		int oid=partyMember.getOrg().getOid();
		OrganizationInformationDto orginfd=organizationService.showOrganizationInformation(oid);
		m.addAttribute("orginfd", orginfd);
		return "organizationInformationEdit";
	}
	@PostMapping("/updateOrganizationInformation")
	public String updateOrganizationInformation(HttpServletRequest request,HttpSession session,Model m) {
		String oid=request.getParameter("oid");
		String oname=request.getParameter("oname");
		String bname=request.getParameter("bname");
		String linkman=request.getParameter("linkman");
		String ophone=request.getParameter("ophone");
		String partysList=request.getParameter("partysList");
		String address=request.getParameter("address");
		String coordinate=request.getParameter("coordinate");
		Organization org=new Organization();
		org.setOid(Integer.parseInt(oid));
		org.setOname(oname);
		org.setBname(bname);
		org.setLinkman(linkman);
		org.setOphone(ophone);
		org.setAddress(address);
		org.setMembersList(partysList);
		org.setCoordinate(coordinate);
		organizationService.updateOrganization(org);
		PartyMember partyMember=(PartyMember) session.getAttribute("loginPartyMember");
		int oid1=partyMember.getOrg().getOid();
		OrganizationInformationDto orginfd=organizationService.showOrganizationInformation(oid1);
		m.addAttribute("orginfd", orginfd);
		return "organizationInformation";
	}
	@RequestMapping("/showOrganizationMap")
	public String showOrganizationMap(Model m) {
		List<String> list=organizationService.showOrganizationMap();
		m.addAttribute("coordinates", list);
		return "organizationMap";
	}
	/*@RequestMapping("/showOrganizationMap2")
	public String showOrganizationMap2() {
		return "organizationMap2";
	}*/
	

}
